#ifndef _NETWORKDEFINE_H
#define _NETWORKDEFINE_H

#ifndef FIELD_OFFSET
#define FIELD_OFFSET(type, field) ((long long)(void*)&(((type *)0)->field))
#endif // !FIELD_OFFSET

enum NETSIG
{
	NET_ON,
	NET_OFF,
	NET_DATA,
	NET_INFO
};

namespace DH
{
	#pragma pack(push,1) 

	static const char netpack_magic[] = "test_baotou";
	struct PACKAGE_INFO
	{
		char magic[sizeof(netpack_magic)];				//包头标志
		unsigned long version;							//协议版本号
		unsigned long packNO;							//包编号
		unsigned long srcMachine;						//发送机器-6
		unsigned long dstMachine;						//目的机器
		unsigned long flags;							//标志
		unsigned long packtype;							//包类型
		unsigned long datalen;							//数据长度
		unsigned long checkcode;						//头校验
		char data[1];							//数据，根据包类型解释

		PACKAGE_INFO(unsigned long dwDataLen, unsigned long src = MACHINE_SERVER, 
			unsigned long dst = MACHINE_CHECKIPC1, unsigned long packType = PT_BASEINFO, unsigned long flags=0)
		{
			static unsigned long no = 1;
			memcpy(magic, netpack_magic, sizeof(netpack_magic));
			version = NETWORK_VERSION;
			packNO = no++;
			srcMachine = src;
			dstMachine = dst;
			packtype = packType;
			flags = flags;
			datalen = dwDataLen;
			checkcode = GetCheckSum(this, FIELD_OFFSET(PACKAGE_INFO, checkcode));
		}
	public:
		unsigned long PACKAGE_INFO::GetCheckSum(void* pData, int nLen)
		{
			unsigned long nRst = 0;
			for (int i = 0; i < nLen; i++)
				nRst += ((unsigned char*)pData)[i];
			return nRst;
		}
	};
	#pragma pack(pop)
}
#endif
